package com.ayu.a.practice01;

import java.util.ArrayList;

/**
 * @Author 徐林玉
 * @Description MoveZeroPrac
 * @Date 2022/11/9 9:46
 * @Version 1.0
 */
public class MoveZeroPrac {


    public static void main(String[] args) {

        int[] nums = new int[]{0, 1, 0, 3, 12};

        moveZero2(nums);

        for (int i = 0; i < nums.length; i++) {


            System.out.print(nums[i] + "  ");
        }

    }

    public static void moveZero(int[] nums) {

        ArrayList<Integer> arr = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {

            if (nums[i] != 0) {

                arr.add(nums[i]);
            }

        }

        for (int i = 0; i < nums.length; i++) {

            if (i < arr.size()) {
                nums[i] = arr.get(i);
            } else {
                nums[i] = 0;
            }

        }
    }


    /**
     * 双指针
     */
    public static void moveZero2(int[] nums){


        int index = 0;


        for (int i = 0; i < nums.length; i++) {

            if(nums[i]!=0){

               nums[index++] = nums[i];
           }

        }

        for (int i = index; i < nums.length; i++) {

            nums[i] = 0;
        }

    }

}
